package com.jml.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jml.mapper.RoleMapper;
import com.jml.mapper.UserMapper;
import com.jml.pojo.Role;
import com.jml.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo {

	@Autowired
	private UserMapper userMapper;


	@Autowired
	private RoleMapper roleMapper;

	/**
	 * 满足条件就把后面的拼接到sql中
	 * @param name
	 * @param adress
	 */
	public void conditionFun(String name,String adress){
		QueryWrapper<User> qw = new QueryWrapper<>();
		if(StringUtils.isNotEmpty(name)){
			qw.like("name",name);
		}

		if(StringUtils.isNotEmpty(adress)){
			qw.like("adress",adress);
		}
		//上面和下面是一样的效果，
		qw.like(StringUtils.isNotEmpty(name),"name",name)
				.like(StringUtils.isNotEmpty(adress),"adress",adress);
		userMapper.selectList(qw);
	}

	@Test
	public void conditionTest(){

	}

	@Test
	public void testUserRole() {
		User user = userMapper.findUserByName("cuihua");
		Set<Role> roles = userMapper.getUserRoles("cuihua");
		for (Role role : roles) {
			role.setPermissions(roleMapper.getRolePermissions(role.getRoleCode()));
		}
		user.setRoles(roles);
		System.out.println(user);
	}

	@Test
	public void testSelect001(){
		//
		User user = new User();
		System.out.println("user:--"+user);
		//批量根据ID查找
		List<Integer> integers = Arrays.asList(1, 2, 3);
		List<User> users = userMapper.selectBatchIds(integers);
		users.forEach(a -> System.out.println(a));
		//以map的形式查找
		Map<String,Object> map = new HashMap<>();
		map.put("name","cuihua");
		List<User> users1 = userMapper.selectByMap(map);
		users1.forEach(a -> System.out.println(a));
		//查找名字包含hua而且年龄小于18的用户
		QueryWrapper<User> qw = new QueryWrapper<>();
		qw.like("name","hua").lt("age",18);
		User user1 = userMapper.selectOne(qw);
		QueryWrapper<User> qw1 = new QueryWrapper<>();
		//查找名字包含hua,age在25到50之间，并且email字段不为null
		qw1.like("name","hua").between("age",25,50).isNotNull("email");
		//右模糊，或者  ,排序
		qw1.likeRight("name","hua").or().ne("","").orderByAsc("");
		//查找罗姓，并且（年龄小于30，邮箱不为空）
		qw1.likeRight("name","罗").and(aa -> aa.lt("age",30).isNotNull("email"));
		//查找罗姓，或者（年龄小于30，邮箱不为空）
		qw1.likeRight("name","罗").or(aa -> aa.lt("age",30).isNotNull("email"));
		//查询满足条件的，如果有多条数据就只取其中一条数据
		qw1.likeRight("name","罗").last("limit 1");
		//指定查询的列名
		qw1.select("列名1","列名2","列名3").likeRight("name","罗");
		//查询年龄是18,19,20的人
		qw1.in("age",Arrays.asList(18,19,20));

	}
	@Test
	public void testSelect(){
		// 此处 null 指的是不用根据参数去查询
		// 可以调用 CRUD 相关的多种方式
		// 1. 查询所有的数据
		List<User> userList = userMapper.selectList(null);
		userList.forEach(user -> System.out.println(user.getName()));
		// 2. 根据 id 删除
		userMapper.deleteById(1);
		// 3. 添加数据
		User user = new User();
		user.setName("老王");
		user.setEmail("laowang@jml.com");
		user.setAge(18);
		userMapper.insert(user);
		// 4. 更新数据（此时插入的是）
		user.setName("老王王");
		user.setEmail("laowangwang@jml.com");
		userMapper.updateById(user);
	}
}
